<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="header.jsp" %>
<%@include file="footer.jsp" %>
<%@include file="functions.jsp" %>
<%@page import="web.vungsi"%>
<%@page import="java.sql.*"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <h1>Ini halaman utama lho</h1>
        <%
            Cookie cookies[] = request.getCookies();
            Cookie myCookie = null;
            if (cookies != null) {
                for (int i = 0; i < cookies.length; i++) {
                    if (cookies[i].getName().equals("id")) {
                        myCookie = cookies[i];
                        break;
                    }
                }
            }
            
            int id_login =0;
            if (myCookie!=null){
                id_login = Integer.parseInt(myCookie.getValue());
            }

            int i = 0;
            int aidi = 0;
            int like_count = 0;
            int komen_count = 0;
            String title = "";
            String content = "";

            //konek ke sql
            String koneksiURL = "jdbc:mysql://localhost/progin_171_13509024?user=progin&password=progin";
            Connection koneksi = null;
            Statement statement = null;
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            koneksi = DriverManager.getConnection(koneksiURL);
            statement = koneksi.createStatement();

            //konten dgn 3 like terbanyak
            out.write("<h3>Tiga Konten Terbaik berdasarkan Jumlah Like Tertinggi<br></h3>");
            ResultSet ambilLikeCount = koneksi.createStatement().executeQuery("SELECT * FROM tbl_post ORDER BY like_count DESC LIMIT 3");
            for (i = 0; i <= 2; i++) {
                if (ambilLikeCount.next()) {
                    out.println(getContent(id_login, ambilLikeCount.getInt("id")));
                    //title = ambilLikeCount.getString("title");
                    //content = ambilLikeCount.getString("content");
                    //like_count = ambilLikeCount.getInt("like_count");
                    //out.write("<h3><a href>" + title + "</a></h3>" + vungsi.ambilKonten(content) + "<br>Jumlah Like : " + like_count + "<br><br>");
                }
            }
            out.write("<br><br><br>");

            //konten dgn 3 like terbanyak
            out.write("<h3>Tiga Konten Terbaik berdasarkan Jumlah Komentar Terbanyak<br></h3>");
            ResultSet ambilKomeng = koneksi.createStatement().executeQuery("Select `post_id`, count(`content`) as count from `tbl_comment` group by `post_id` order by count DESC");
            for (i = 0; i <= 2; i++) {
                if (ambilKomeng.next()) {
                    aidi = ambilKomeng.getInt("post_id");
                    out.println(getContent(id_login, aidi));
                }
            }
            out.write("<br><br><br>");

            out.write("<center><BLINK><a href>[TONO-lik]Dijual pulsa elektronik. MURAH!</a></BLINK>");
        %>
    </body>
</html>
